//
//  GWHomeViewController.m
//  GiftWanted
//
//  Created by lanou on 15/11/5.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWHomeViewController.h"
#import "GWNavigationController.h"
#import "RS3DSegmentedControl.h"
#import "GWHandpickTableViewController.h"
#import "GWOtherTableViewController.h"
#import "GWItemsModel.h"

@interface GWHomeViewController () <RS3DSegmentedControlDelegate>

@property (nonatomic, strong) RS3DSegmentedControl *segmentedControl;
@property (nonatomic, strong) GWHandpickTableViewController *handpickVC;
@property (nonatomic, strong) GWOtherTableViewController *otherVC;

//@property (nonatomic, strong) NSMutableArray *dataSourceArray;

@end

@implementation GWHomeViewController

// 设置RS3DSegementedControl
- (void)loadView {
    
    [super loadView];
    
    self.segmentedControl = [[RS3DSegmentedControl alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, 30)];
    _segmentedControl.delegate = self;
    _segmentedControl.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_segmentedControl];
}

- (NSUInteger)numberOfSegmentsIn3DSegmentedControl:(RS3DSegmentedControl *)segmentedControl {
    return 6;
}

- (NSString *)titleForSegmentAtIndex:(NSUInteger)segmentIndex segmentedControl:(RS3DSegmentedControl *)segmentedControl {
    switch (segmentIndex) {
        case 0:
            return @"礼物";
            break;
            
        case 1:
            return @"美食";
            break;
            
        case 2:
            return @"数码";
            break;
            
        case 3:
            return @"运动";
            break;
            
        case 4:
            return @"涨姿势";
            break;
            
        default:
            return @"精选";
            break;
    }
    
}

- (void)didSelectSegmentAtIndex:(NSUInteger)segmentIndex segmentedControl:(RS3DSegmentedControl *)segmentedControl {
    
    switch (segmentIndex) {
        case 0:
    
            _otherVC.urlStr = @"http://api.liwushuo.com/v2/channels/111/items?ad=1&gender=1&generation=1&limit=20&offset=0";
            [_otherVC configureData];
            [self.view bringSubviewToFront:_otherVC.view];
            break;
        case 1:
        
            _otherVC.urlStr = @"http://api.liwushuo.com/v2/channels/118/items?ad=1&gender=1&generation=1&limit=20&offset=0";
            [_otherVC configureData];
            [self.view bringSubviewToFront:_otherVC.view];
            break;
        case 2:
           
            _otherVC.urlStr = @"http://api.liwushuo.com/v2/channels/121/items?ad=1&gender=1&generation=1&limit=20&offset=0";
            [_otherVC configureData];
            [self.view bringSubviewToFront:_otherVC.view];
            break;
        case 3:
            
            _otherVC.urlStr = @"http://api.liwushuo.com/v2/channels/123/items?ad=1&gender=1&generation=1&limit=20&offset=0";
            [_otherVC configureData];
            [self.view bringSubviewToFront:_otherVC.view];
            break;
        case 4:
            
            _otherVC.urlStr = @"http://api.liwushuo.com/v2/channels/120/items?ad=1&gender=1&generation=1&limit=20&offset=0";
            [_otherVC configureData];
            [self.view bringSubviewToFront:_otherVC.view];
            break;
        case 5:

            [self.view bringSubviewToFront:_handpickVC.view];
            break;
            
        default:
            break;
    }

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.title = @"首页";
//    self.navigationItem.title = @"首页";
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu_3"] style:UIBarButtonItemStylePlain target:(GWNavigationController *)self.navigationController action:@selector(showMenu)];
//    self.view.backgroundColor = [UIColor yellowColor];
    //    self.titleLabel.text = @"首页";
    [self p_setupTopView];
    
    _otherVC = [[GWOtherTableViewController alloc] init];
    _otherVC.naviC = self.navigationController;
    _otherVC.view.frame = CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HEIGHT - 150);
    [self.view addSubview:_otherVC.view];
    [self addChildViewController:_otherVC];
    
    _handpickVC = [[GWHandpickTableViewController alloc] init];
    _handpickVC.naviC = self.navigationController;
    _handpickVC.view.frame = CGRectMake(0, 100, SCREEN_WIDTH, SCREEN_HEIGHT - 100);
    [self.view addSubview:_handpickVC.view];
    _handpickVC.urlStr = @"http://api.liwushuo.com/v2/channels/100/items?ad=1&gender=1&generation=1&limit=20&offset=0";
    [_handpickVC configureData];
}

//- (NSMutableArray *)dataSourceArray {
//    
//    if (!_dataSourceArray) {
//        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
//    }
//    return _dataSourceArray;
//}

//- (void)configureDataWithURLStr:(NSString *)str {
//
//    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
//    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//        [self.dataSourceArray removeAllObjects];
//        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"%@", dict);
//        for (NSDictionary *modelDic in dict[@"data"][@"items"]) {
//            GWItemsModel *model = [[GWItemsModel alloc] init];
//            [model setValuesForKeysWithDictionary:modelDic];
//            [self.dataSourceArray addObject:model];
//        }
//        [self.handpickVC.tableView reloadData];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"网络请求失败");
//    }];
//    [operation start];
//}

- (void)p_setupTopView {
    
    self.navigationController.navigationBar.hidden = YES;
//    self.titleImage.image = [UIImage imageNamed:@"Home"];
    self.titleLabel.text = @"首页";
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"menu_3"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:(GWNavigationController *)self.navigationController action:@selector(showMenu) forControlEvents:(UIControlEventTouchUpInside)];
//    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"彩灯"]];
//    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView.backgroundColor = [UIColor colorWithRed:1.000 green:0.379 blue:0.396 alpha:1.000];
//    imageView.frame = CGRectMake(0, SCREEN_HEIGHT-50, SCREEN_WIDTH, 50);
//    [self.view addSubview:imageView];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
